const express = require('express');
const db = require('../../utils/db');
const multer = require('multer')
const login = express.Router();

login.all('*',function(req,res,next){
    // next();
    if(req.path=='/sider/login'){
        next()
    }else{
        if(req.session.username!=undefined){
            next();
        }else{
            res.redirect('/pages/sider_nav/login.html')
        }
    }
})

login.all('/sider/login', (req, res) => {
    let sql = `select * from gxa_3.users where username = '${req.body.username}' and password='${req.body.password}'`
    db.query(sql, function(err, data) {
        if (err) {
            console.log(err);
        } else {
            if(data.length){
                req.session.username=data[0].username;
                req.session.userid=data[0].userid;
                res.send({state:1,msg: '登录成功',username:data[0].username,userid:data[0].userid})
            }else{
                res.send({state:0,msg:'登录失败'})
            }
        }
    })
})
login.post('/sider/changepwd',(req,res)=>{
    let sql = `update users set password=${req.body.newpwd} where userid=${req.session.userid} and password=${req.body.password}`
    db.query(sql,function(err,data){
        if (err) {
            console.log(err);
        } else {
            if(data.changedRows){
                res.send({state:1,msg: '修改成功' })
            }else{
                res.send({state:0,msg:'修改失败'})
            }
        }
    })
})
login.post('/sider/msg',(req,res)=>{
    let sql = `select u.img,yuangong.* from (select * from users where userid=${req.body.userid}) as u,yuangong where u.y_id = yuangong.y_id`
    db.query(sql,function(err,data){
        if (err) {
            console.log(err);
        } else {
            if(data.length){
                let sql1 = `select * from salary_level where l_id=${data[0].y_zhicheng}`
                db.query(sql1,function(err,datas){
                    if(err){
                        console.log(err);
                    }else{
                        if(datas.length){
                            data[0].y_zhicheng=datas[0].l_title
                            res.send({state:1,msg:'查询成功',data:data[0]})
                        }
                    }
                })
            }else{
                res.send({state:0,msg:'查询失败'})
            }
        }
    })
})
login.post('/sider/setmsg',(req,res)=>{
    let data=req.body.data
    let sql = `update yuangong set y_sex='${data.y_sex}',y_name='${data.y_name}',
        y_shengri='${data.y_shengri}',y_youxiang='${data.y_youxiang}',
        y_tel='${data.y_tel}',y_weixin='${data.y_weixin}',
        y_gonghao='${data.y_gonghao}',y_yuanxiao='${data.y_yuanxiao}',
        y_biye='${data.y_biye}',y_zhuanye='${data.y_zhuanye}',
        y_xueli='${data.y_xueli}',y_beizhu='${data.y_beizhu}' 
        where (select y_id from users where userid=${req.body.userid})=y_id`
    db.query(sql,function(err,data){
        if (err) {
            console.log(err);
        } else {
            if(data.changedRows){
                res.send({state:1,msg: '修改成功' })
            }else{
                res.send({state:0,msg:'修改失败'})
            }
        }
    })
})
login.post('.sider/logout',(req,res)=>{
    req.session.username=''
    req.session.userid=''
})
//重命名
const storage = multer.diskStorage({
    destination:function(res,file,cb){
        console.log(file);
        cb(null,'./public/images')
    },
    filename:function(res,file,cb){
        cb(null,res.session.username+'.jpg')
    }
})
//解析
const upload = multer({storage:storage})
//single单个上传
login.post('/sider/upload',upload.single('file'),function(req,res){
    res.send(req.file.filename)
})
module.exports = login